<template>
    <div class="wrapper">
        <div class="header">
            <el-row style="margin-bottom:20px">
                <el-button type="danger" style="width:120px" @click="centerDialogVisible=true;title='新增'">新建</el-button>
            </el-row>
             <el-row>
                <el-col :span="4" style="margin-right:30px">
                    <el-input  v-model="searchBo.Name" placeholder="推送器名称"></el-input>
                </el-col> <el-button type="danger" style="width:120px" @click="getList">查询</el-button>
            </el-row>
        </div>
        <div class="body">
            <Table :list="list" :state="false" v-loading="loading" :total="total" @currentChange="currentChange" @sizeChange="handSize">
                <slot>
                     <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                @click="handleEdit(scope.row)">修改
                            </el-button>
                            <el-button
                                size="mini"
                                type="danger"
                                @click="handleDel(scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </slot>
            </Table>
        </div>
        <el-dialog
            :title="title"
            :visible.sync="centerDialogVisible"
             :before-close="onCancel"
            width="30%"
            >
            <div>
                <el-form ref="addForm" :model="addForm" :rules="rule" label-position="right" label-width="80px">
                    <el-form-item label="设备ID" prop='id'>
                        <el-input v-model="addForm.id" placeholder="请输入设备ID"></el-input>
                    </el-form-item>
                    <el-form-item label="设备别名" prop='name'>
                        <el-input v-model="addForm.name" placeholder="请输入设备别名"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop='remark'>
                        <el-input v-model="addForm.remark" placeholder="请输入备注"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="onCancel">取 消</el-button>
                <el-button type="primary" @click="onOk">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Socket from '../../../utils/socket'
import Table from '@/components/Table'
import { getToken } from '../../../utils/auth'
import {pusherInfoList,pusherInfoEdit,pusherInfoAdd,pusherInfoDel} from '../../../api/pusher'
export default {
    components:{
        Table
    },
    data () {
        return {
            searchBo:{},
            list:{
                thead:[
                    {label:'推送器编号',prop:"id",rowspan:1},
                    {label:'推送器名称',prop:"name",rowspan:1},
                    {label:'备注',prop:"remark",rowspan:1},
                    {label:'连接状态',prop:"status",rowspan:1,formatter:(row)=>{
                        switch(row.status) {
                            case 0:
                                return "离线"
                            case 1:
                                return "在线"
                        }
                    }},
                ],
                tbody:[]
            },
            loading:false,
            total:0,
            title:'新增',
            centerDialogVisible:false,
             addForm:{
                id:'',
                name:'',
                remark:''
            },
            rule:{
                id:[  { required: true, message: '请输入设备ID', trigger: 'blur' },],
                name:[  { required: true, message: '请输入设备别名', trigger: 'blur' },],
                remark:[  { required: true, message: '请输入备注', trigger: 'blur' },]
            },
            pageNum:1,
            pageSize:20,
            socket:null

        }
        
    },
    destroyed() {
        this.socket.close()
    },
    mounted () {
        this.getList();
         this.socket = new Socket({url:'ws://dev-ws.sqcat.cn/pusher-client/'+getToken(),callback:this.onMessage});
    },
    methods:{
          onMessage (data) {
              this.getList()
          },
          onOk () {
            this.$refs['addForm'].validate((vaild)=>{
                if(vaild) {
                    if(this.title == '新增'){
                        pusherInfoAdd(this.addForm).then(res=>{
                            if(res.status == 200) {
                                this.$message({
                                    type:'success',
                                    message:"新增成功！"
                                })
                                this.getList()
                                this.onCancel()
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }else{
                         pusherInfoEdit(this.addForm).then(res=>{
                            if(res.status == 200) {
                                this.$message({
                                    type:'success',
                                    message:"修改成功！"
                                })
                                this.getList()
                                this.onCancel()
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
           
        },
        onCancel () {
            this.centerDialogVisible = false;
            this.addForm=this.$options.data().addForm;
            this.$refs['addForm'].clearValidate();
            this.$refs['addForm'].resetFields();
        },
        getList () {
            this.loading =true;
            let obj = {
                pageNum:this.pageNum,
                pageSize:this.pageSize,
                ...this.searchBo,
                
            }
            pusherInfoList(obj).then(res=>{
                if(res.status == 200) {
                    this.total = res.data.total;
                    this.list.tbody = res.data.list;
                }else{
                    this.$message.error(res.msg)
                }
            }).finally(res=>{
                this.loading = false;
            })
        },
        handSize (data) {
            this.pagege = data;
            this.getList()
        },
        currentChange (val) {
            this.pageSize = val;
            this.getList()
        },
        handleEdit (row) {
            this.title = '修改'
            this.addForm = {...row};
            this.centerDialogVisible = true;
        },
        handleDel (row) {
             this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    pusherInfoDel(row).then(res=>{
                        if(res.data) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.getList()
                        }else{
                            this.$message({
                                type:'error',
                                message:res.msg
                            })
                        }
                    })
                    
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        }
    }
}
</script>

<style lang="less" scoped>
    .wrapper{
        width: 100%;
         overflow: auto;
        .header{
            height: 190px;
            margin-bottom: 25px;
            background-color: #fff;
            padding:38px 20px;
            box-sizing: border-box;;
        }
        .body{
            width: 100%;
            box-sizing: border-box;
            padding:10px 20px;
            height:500px ;
            overflow: auto;
            background-color: #fff;
        }

    }
</style>